#ifndef _WORK_H_
#define _WORK_H_

#include "ddl.h"
#include "gpio.h"
#include "adc.h"

#define RX_PORT				GPIOA
#define RX_PIN				GPIO_PIN_01
#define TX_PORT				GPIOA
#define TX_PIN				GPIO_PIN_02

#define ADC_VBAT_PORT		GPIOB
#define ADC_VBAT_PIN		GPIO_PIN_05

#define STDBY_PORT			GPIOA
#define STDBY_PIN 			GPIO_PIN_03

#define EN_VBAT_PORT		GPIOB
#define EN_VBAT_PIN			GPIO_PIN_02

#define STAT_PORT			GPIOC
#define STAT_PIN			GPIO_PIN_03
#define BUZZER_PORT			GPIOB
#define BUZZER_PIN			GPIO_PIN_00
	// 第1组pwm的升压pwm脚
#define PWM_EMS1_PORT		GPIOB		
#define PWM_EMS1_PIN		GPIO_PIN_04

#define EMS1_PWM_N_PORT 	GPIOC
#define EMS1_PWM_N_PIN		GPIO_PIN_04

#define EMS1_PWM_P_PORT 	GPIOD
#define EMS1_PWM_P_PIN		GPIO_PIN_03
	// 第2组pwm
#define PWM_EMS2_PORT		GPIOB		
#define PWM_EMS2_PIN		GPIO_PIN_01

#define EMS2_PWM_N_PORT 	GPIOC
#define EMS2_PWM_N_PIN		GPIO_PIN_05

#define EMS2_PWM_P_PORT 	GPIOD
#define EMS2_PWM_P_PIN		GPIO_PIN_02
//==================
#define DI_PORT				GPIOC
#define DI_PIN				GPIO_PIN_06

#define EMS_ISEN_PORT		GPIOD
#define EMS_ISEN_PIN		GPIO_PIN_05

#define USB_IN_PORT			GPIOD
#define USB_IN_PIN			GPIO_PIN_06

//----------按键pin脚-------------------
#define K1_PORT				GPIOD
#define K1_PIN				GPIO_PIN_04

#define K2_USE_RESET_PIN	1
#define K2_USE_PA4_PIN		2
#define K2_USE_PIN			K2_USE_PA4_PIN

#if(K2_USE_PIN	== K2_USE_RESET_PIN)
	#define K2_PORT			GPIOA
	#define K2_PIN			GPIO_PIN_00
#else
	#define K2_PORT			GPIOA
	#define K2_PIN			GPIO_PIN_04
#endif

#define K3_PORT				GPIOB
#define K3_PIN				GPIO_PIN_03


#define USB_IN				REG_READBITS(USB_IN_PORT->IN, USB_IN_PIN)


#define EN_VBAT_ON			REG_WRITE(EN_VBAT_PORT->BSRR, EN_VBAT_PIN)
#define EN_VBAT_OFF			REG_WRITE(EN_VBAT_PORT->BRR, EN_VBAT_PIN)
#define STDBY				REG_READBITS(STDBY_PORT->IN, STDBY_PIN)
#define STAT				REG_READBITS(STAT_PORT->IN, STAT_PIN)
#define BUZZER_ON			REG_WRITE(BUZZER_PORT->BSRR, BUZZER_PIN)
#define BUZZER_OFF			REG_WRITE(BUZZER_PORT->BRR, BUZZER_PIN)

#define DI_ON				REG_WRITE(DI_PORT->BSRR, DI_PIN)
#define DI_OFF				REG_WRITE(DI_PORT->BRR, DI_PIN)

#define EMS1_PWM_P_ON		REG_WRITE(EMS1_PWM_P_PORT->BSRR, EMS1_PWM_P_PIN)
#define EMS1_PWM_P_OFF		REG_WRITE(EMS1_PWM_P_PORT->BRR, EMS1_PWM_P_PIN)
#define EMS1_PWM_N_ON		REG_WRITE(EMS1_PWM_N_PORT->BSRR, EMS1_PWM_N_PIN)
#define EMS1_PWM_N_OFF		REG_WRITE(EMS1_PWM_N_PORT->BRR, EMS1_PWM_N_PIN)

#define EMS2_PWM_P_ON		REG_WRITE(EMS2_PWM_P_PORT->BSRR, EMS2_PWM_P_PIN)
#define EMS2_PWM_P_OFF		REG_WRITE(EMS2_PWM_P_PORT->BRR, EMS2_PWM_P_PIN)
#define EMS2_PWM_N_ON		REG_WRITE(EMS2_PWM_N_PORT->BSRR, EMS2_PWM_N_PIN)
#define EMS2_PWM_N_OFF		REG_WRITE(EMS2_PWM_N_PORT->BRR, EMS2_PWM_N_PIN)


#define EMS_ISEN			REG_READBITS(EMS_ISEN_PORT->IN, EMS_ISEN_PIN)
#define K1					REG_READBITS(K1_PORT->IN, K1_PIN)
#define K2					REG_READBITS(K2_PORT->IN, K2_PIN)
#define K3					REG_READBITS(K3_PORT->IN, K3_PIN)

#define ADC_VBAT_Channel	ADC_EXINPUT_PB05
#define EMS_ISEN_Channel	ADC_EXINPUT_PD05

#define PWM_FREQ			250000//15000


#define LED_BRIGHT			6//25		//30
#define GREEN_LED_BRIGHT	(LED_BRIGHT - 1)  //因为绿光比较刺眼，所以绿光亮度-1

enum
{
		Trigger_Start=1,
		Trigger_End=0,
};

enum
{
		Voltage_Over=3,
		Voltage_Normal=2,
		Voltage_Warning=1,
		Voltage_Under=0,
};

enum
{
		PWM_Channel,
		FAN_Channel,
};

typedef enum
{
	SLEEP_MODE = 0,
	EMS_MODE1,
	EMS_MODE2,
	EMS_MODE3,
	EMS_MODE4,

	ERR_MODE,		// ERR
}WORK_MODE_e;


void User_Init(void);
void Work_Handler(void);
void Trigger_Handle(uint16_t duty);
void Voltage_Detect(void);
void Voltage_Control(uint16_t Target_Voltage);
void Input_Voltage_Protect(void);
void Work_CountTime(void);
// void DEBUG_Handler(void);
void BAT_Handler(void);
void Charge_Handler(void);
void Beep_Handler(void);
void Halt_Handler(void);

#endif
